//
//  EditNameController.m
//  CaiPiao
//
//  Created by l wb on 2018/5/17.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "EditNameController.h"

@interface EditNameController ()
@property (weak, nonatomic) IBOutlet UITextField *displayNameTF;

@end

@implementation EditNameController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"修改姓名";
    [self addRightNavBarBtn];
    
}
- (void)addRightNavBarBtn
{
    UIButton* _btnRight = [UIButton buttonWithType:UIButtonTypeCustom];
    [_btnRight setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [_btnRight setTitle:@"保存" forState:UIControlStateNormal];
    [_btnRight setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_btnRight setBounds:CGRectMake(0, 0, 50, 44)];
    UIBarButtonItem* Rightitem  = [[UIBarButtonItem alloc] initWithCustomView:_btnRight];
    [_btnRight addTarget:self action:@selector(rightButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = Rightitem;
}
#pragma mark - 修改用户名
- (void)rightButtonClickAction
{
    
    if (self.displayNameTF.text.length==0) {
        [EasyTextView showText:@"请输入名字"];
        return;
    }
    //查找GameScore表
    BmobQuery   *bquery = [BmobQuery queryWithClassName:@"_User"];
    //查找GameScore表里面id为0c6db13c的数据
    [bquery getObjectInBackgroundWithId:kUserID block:^(BmobObject *object,NSError *error){
        //没有返回错误
        if (!error) {
            //对象存在
            if (object) {
                

                BmobObject *objectUser = [BmobObject objectWithoutDataWithClassName:object.className objectId:object.objectId];
                
                
                //设置cheatMode为YES
                [objectUser setObject:self.displayNameTF.text forKey:@"displayName"];
                //异步更新数据
                [objectUser updateInBackground];
                [EasyTextView showSuccessText:@"修改成功"];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.navigationController popViewControllerAnimated:YES];
                });
                
            }
        }else{
            //进行错误处理
        }
    }];
    
    
    
}

@end
